//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_updateRespawnLocations.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);

private ["_dlg_type","_list","_list_names","_list_units","_n","_side","_current_selection","_current_name","_current_object","_i"];
DEFAULT_PARAM(_dlg_type,0,0);

_list = [_dlg_type] call BTCR_fnc_getRespawnLocations;
_list_names = _list select 0;
_list_units = _list select 1;
_n = 0;

_current_selection = lbCurSel 119;
_current_name = "";
_current_object = objNull;
if (_current_selection >= 0) then {
	TRACE_1("Previous Selection",ARR_3(lbCurSel 119,_current_selection,BTC_r_list_names));
	_current_name = BTC_r_list_names select _current_selection;
	_current_object = BTC_r_list_objects select _current_selection;
} else {
	if (!isnil "BTC_r_mobile_selected" && count _list_names > 0) then {
		for "_i" from 0 to ((count _list_units) - 1) step 1 do {
			if (typename BTC_r_mobile_selected == typename (_list_units select _i) && {BTC_r_mobile_selected == (_list_units select _i)}) exitWith {
				TRACE_1("Previous Selection",ARR_3(lbCurSel 119,_i,_list_names));
				_current_selection = _i;
				_current_name = _list_names select _i;
				_current_object = BTC_r_mobile_selected;
			};
		};
	};
};
TRACE_1("Current Selection",ARR_4(lbCurSel 119,_current_selection,_current_name,_current_object));

BTC_r_list_objects = _list_units;
if (count _list_names != count BTC_r_list_names) then 
{
	DEBUG_1("Update List",_list_names);
	lbClear 119;
	{
		_lb = lbAdd [119,_x];
		if (_current_name != "" && {_current_name == _x}) then {
			_current_selection = _lb;
		};
	} foreach _list_names;
	if (_current_selection < 0) then {	_current_selection = 0;	};
	lbSetCurSel [119,_current_selection];
	BTC_r_list_names = _list_names;
} else {
};
TRACE_1("End",lbCurSel 119);
true
